package com.sfzlot.dev.netty.controller;

import com.sfzlot.dev.netty.bean.Message;
import com.sfzlot.dev.netty.bean.bussiness.Industry;
import com.sfzlot.dev.netty.bean.bussiness.Product;
import com.sfzlot.dev.netty.bean.bussiness.Subdivide;
import com.sfzlot.dev.netty.bean.command.PowerSwitchCommand;
import com.sfzlot.dev.netty.bean.data.DeviceInfoInData;
import com.sfzlot.dev.netty.bean.data.DeviceLogoutInData;
import com.sfzlot.dev.netty.bean.data.DeviceServerInData;
import com.sfzlot.dev.netty.bean.data.DeviceSettingInData;
import com.sfzlot.dev.netty.bean.data.powerswitch.PowerSwitchStartInData;
import com.sfzlot.dev.netty.bean.data.powerswitch.PowerSwitchStopInData;
import com.sfzlot.dev.netty.bean.data.powerswitch.PowerSwitchSwitchInData;
import com.sfzlot.dev.netty.bean.identifier.EncryptMethod;
import com.sfzlot.dev.netty.bean.identifier.ResponseIdentifier;
import com.sfzlot.dev.netty.utils.CommandUtils;
import com.sfzlot.dev.netty.utils.MessageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Random;

@Api(tags = "Netty调用开关接口")
@RestController
@CrossOrigin("*")
@RequestMapping(value = "/api/command")
public class CommandRestController {
    /**
     * 设备登出
     * @param vin
     */
    @ApiOperation(value = "设备登出")
    @PostMapping(value = "/logout")
    public String logout(@RequestParam(required = true) String vin){
        Message message = getMessage(vin);
        message.setCommandIdentifier(PowerSwitchCommand.DEVICE_LOGOUT);
        DeviceLogoutInData deviceLogoutInData = new DeviceLogoutInData();
        // 读取时间
        deviceLogoutInData.setDataCollectTime(new Date());
        // 设备登录时登入流水号
        deviceLogoutInData.setSerialNum(999);
        message.setData(deviceLogoutInData);
        CommandUtils.cmd(message);
        return MessageUtils.getHexString(message);
    }

    /**
     * 更改服务器
     * @param vin
     * @param url
     */
    @ApiOperation(value = "更改服务器")
    @PostMapping(value = "/updateServer")
    public String updateServer(@RequestParam(required = true)String vin,@RequestParam(required = true)String url){
        Message message = getMessage(vin);
        message.setCommandIdentifier(PowerSwitchCommand.DEVICE_SERVER);
        DeviceServerInData deviceServerInData = new DeviceServerInData();
        deviceServerInData.setServerUrl(url);
        message.setData(deviceServerInData);
        CommandUtils.cmd(message);
        return MessageUtils.getHexString(message);
    }

    /**
     * 更改初使值
     * @param vin
     * @param ssid
     * @param password
     */
    @ApiOperation(value = "更改初使值")
    @PostMapping(value = "/updateSettingIn")
    public String updateSettingIn(@RequestParam(required = true)String vin,@RequestParam(required = true)String ssid,@RequestParam(required = true)String password){
        Message message = getMessage(vin);
        message.setCommandIdentifier(PowerSwitchCommand.DEVICE_SETTING);
        DeviceSettingInData deviceSettingInData = new DeviceSettingInData();
        deviceSettingInData.setSsid(ssid);
        deviceSettingInData.setPassword(password);
        message.setData(deviceSettingInData);
        CommandUtils.cmd(message);
        return MessageUtils.getHexString(message);
    }

    /**
     *  获取终端信息
     * @param vin
     */
    @ApiOperation(value = "获取终端信息")
    @PostMapping(value = "/info")
    public String info(@RequestParam(required = true)String vin){
        Message message = getMessage(vin);
        message.setCommandIdentifier(PowerSwitchCommand.DEVICE_INFO);
        DeviceInfoInData deviceInfoInData = new DeviceInfoInData();
        deviceInfoInData.setDataCollectTime(new Date());
        message.setData(deviceInfoInData);
        return CommandUtils.cmd(message);
    }

    /**
     * 通电
     * @param vin
     * @param times
     */
    @ApiOperation(value = "通电")
    @PostMapping(value = "/start")
    public String start(@RequestParam(required = true)String vin,@RequestParam(required = true)String times){
        Message message = getMessage(vin);
        message.setCommandIdentifier(PowerSwitchCommand.DEVICE_START);
        PowerSwitchStartInData deviceStartInData = new PowerSwitchStartInData();
        deviceStartInData.setDataCollectTime(new Date());
        deviceStartInData.setTimes(Integer.parseInt(times));
        message.setData(deviceStartInData);
        CommandUtils.cmd(message);
        return MessageUtils.getHexString(message);
    }

    /**
     * 断电
     */
    @ApiOperation(value = "断电")
    @PostMapping(value = "/stop")
    public String stop(@RequestParam(required = true)String vin,@RequestParam(required = true)String times){
        Message message = getMessage(vin);
        message.setCommandIdentifier(PowerSwitchCommand.DEVICE_STOP);
        PowerSwitchStopInData deviceStopInData = new PowerSwitchStopInData();
        deviceStopInData.setDataCollectTime(new Date());
        deviceStopInData.setTimes(Integer.parseInt(times));
        message.setData(deviceStopInData);
        CommandUtils.cmd(message);
        return MessageUtils.getHexString(message);
    }


    /**
     * 重置设备
     * @param vin
     */
    @ApiOperation(value = "重置设备")
    @PostMapping(value = "/reset")
    public String reset(@RequestParam(required = true)String vin){
        Message message = getMessage(vin);
        message.setCommandIdentifier(PowerSwitchCommand.DEVICE_RESET);
        CommandUtils.cmd(message);
        return MessageUtils.getHexString(message);
    }

    /**
     * 物理开关是否有效
     * @param vin
     * @param flag
     */
    @ApiOperation(value = "物理开关是否有效")
    @PostMapping(value = "/isSwitch")
    public String isSwitch(@RequestParam(required = true)String vin,@RequestParam(required = true)String flag){
        Message message = getMessage(vin);
        message.setCommandIdentifier(PowerSwitchCommand.DEVICE_SWITCH);
        PowerSwitchSwitchInData deviceSwitchInData = new PowerSwitchSwitchInData();
        deviceSwitchInData.setDataCollectTime(new Date());
        deviceSwitchInData.setFlag(Integer.parseInt(flag));
        message.setData(deviceSwitchInData);
        CommandUtils.cmd(message);
        return MessageUtils.getHexString(message);
    }

    private Message getMessage(String vin) {
        Message message = new Message();
        message.setIndustry(Industry.SMARTFAMILY);//行业大类
        message.setSubdivide(Subdivide.SmartFamily.SMARTHOME);//行业细分
        message.setVersion(1);
        message.setVin(vin);
        message.setSerial(new Random().nextInt(65000)+1);
        message.setEncryptMethod(EncryptMethod.NO_ENCRYPTION);

        // 智能开关-登录指令-客户端请求体
        message.setProduct(Product.SmartHome.POWERSWITCH);
        message.setCommandIdentifier(PowerSwitchCommand.DEVICE_LOGIN);
        message.setResponseIdentifier(ResponseIdentifier.COMMAND);

        return message;
    }
}
